package com.utility.project.common.utils;

import java.security.MessageDigest;

/**
 * 加密算法工具类
 * @author nari-yzg
 * @return
 * @date 2021/3/30 12:31
 */
public class SignatureUtil {

    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static final String payload = "";

    /**
     * SHA1算法实现
     * 举例：sha1("yanxuan")=0f6d4fd99e06dcfce7c2a35d30a7652087cf4b70
     * @param origin 明文
     * @return 密文
     */
    public static String sha1(String origin) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(origin.getBytes("UTF-8"));
            byte[] data = md.digest();
            int l = data.length;
            char[] out = new char[l << 1];
            int i = 0;
            for (int j = 0; i < l; ++i) {
                out[(j++)] = DIGITS_LOWER[((0xF0 & data[i]) >>> 4)];
                out[(j++)] = DIGITS_LOWER[(0xF & data[i])];
            }
            return new String(out);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * * 加密公式：
     sha1(sha1(appCode + appSecret) + payload + random + timestamp)
     * @param appCode
     * @param appSecret
     * @return
     */
    public static String generateSignature(String appCode, String appSecret, String payload, Integer random, String timestamp) {
        return sha1(sha1(appCode + appSecret) + payload + random + timestamp);
    }

    public static void main(String[] args) {
        String res = generateSignature("gdt","1f240fc72b4c4c4c804e8c9d076e829d",payload,111,"1620890526442");
        System.out.println(res);
    }
}
